package com.firebird.app.kotlin.stock.ui.regist.presenter

import com.firebird.app.kotlin.stock.base.BasePresenter
import com.firebird.app.kotlin.stock.data.SimpleBean
import com.firebird.app.kotlin.stock.net.RxScheduler.Flo_io_main
import com.firebird.app.kotlin.stock.ui.regist.contract.RegistContract
import com.firebird.app.kotlin.stock.ui.regist.model.RegistModel

import com.firebird.app.kotlin.stock.net.RxScheduler
import okhttp3.RequestBody


class RegistPresenter : BasePresenter<RegistContract.View?>(), RegistContract.Presenter {
    private val model: RegistContract.Model
    override fun getCode(body: RequestBody?) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getCode(body)!!.compose(Flo_io_main<SimpleBean?>())
                .`as`(mView!!.bindAutoDispose()!!)
                .subscribe({ bean ->
                    mView!!.hideLoading()
                    mView!!.onSuccess(bean)
                }) { throwable ->
                    mView!!.hideLoading()
                    mView!!.onError(throwable)
                }
    }

    override fun getData(body: RequestBody?) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getData(body)!!.compose(Flo_io_main<SimpleBean?>())
                .`as`(mView!!.bindAutoDispose()!!)
                .subscribe({ bean ->
                    mView!!.hideLoading()
                    mView!!.onSuccessRegist(bean)
                }) { throwable ->
                    mView!!.hideLoading()
                    mView!!.onError(throwable)
                }
    }

    init {
        model = RegistModel()
    }
}